package com.game.service.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.game.comm.MessageType;
import com.game.mapper.MessageMapper;
import com.game.mapper.PointDetailMapper;
import com.game.mapper.PointLogMapper;
import com.game.modal.InMessage;
import com.game.modal.Message;
import com.game.modal.PointDetail;
import com.game.modal.PointDetailInfo;
import com.game.modal.PointDetailListInfo;
import com.game.modal.PointLog;
import com.game.modal.User;
import com.game.service.IMessageService;
import com.game.service.IProducer;
import com.game.service.IUserService;
import com.game.util.GsonUtils;
import com.game.util.ResultEnum;
import com.game.util.ResultInfo;

@Service
public class MessageService implements IMessageService {

	private static final Logger log = org.slf4j.LoggerFactory.getLogger(MessageService.class);
	@Autowired
	private IUserService userService;
	@Autowired
	private IProducer producer;
	@Autowired
	private MessageMapper messageMapper;
	@Autowired
	private PointDetailMapper pointDetailMapper;
	@Autowired
	private PointLogMapper pointMapper;
	@Autowired
	private RedEnvelopes redEnvlopes;
	@Override
	public void handMsg(InMessage message) {
		
		Map<String,Object> msgData = message.getData();
		log.info("处理消息：{},类型：{}",msgData,message.getType());
		String roomNo = (String)msgData.get("roomId");
		if(MessageType.POINT.equals(message.getType())){
			
			List<User> users = userService.queryByRoomNo(roomNo);
			
			Message msg = new Message();
			Map<String,Object> data = message.getData();
			msg.setMsg(GsonUtils.gsonString(data));
			msg.setSendName(data.get("uname").toString());
			msg.setSendUid(data.get("uid").toString());
			msg.setRoomId(data.get("roomId").toString());
			msg.setDetail(data.get("detail").toString());
			msg.setType(msg.getType());
			int count = messageMapper.insert(msg);
			if(count==0){
				throw new RuntimeException("插入消息错误");
			}
			
			PointLog point = new PointLog();
			point.setRoomId(msg.getRoomId());
			int pointSend = (int) Float.parseFloat(data.get("point").toString());
			int pointNum = Integer.parseInt(data.get("num").toString());
			
			point.setPoint(pointSend);
			point.setSendId(msg.getSendUid());
			point.setNum(pointNum);
			point.setSendName(msg.getSendName());
			point.setDetail(msg.getDetail());
			count = pointMapper.insert(point);
			if(count==0){
				throw new RuntimeException("插入积分错误");
			}
			log.debug("积分对象:{}",point);
			List<Integer> splitPackets = redEnvlopes.splitRedPackets(pointSend, pointNum);
			List<PointDetail> details = new ArrayList<PointDetail>();
			for(Integer p:splitPackets){
				PointDetail detail = new PointDetail();
				detail.setPoint(p);
				detail.setPointId(point.getId());
				details.add(detail);
			}
			log.debug("积分详情：{}",details);
			pointDetailMapper.insertList(details);
			
			msgData.put("pointId", point.getId());
			for(User user:users){
				msgData.put("uid", user.getOpenid());
				producer.sendMessage(GsonUtils.gsonString(message));
			}
		}else if(MessageType.MSG.equals(message.getType())){
			List<User> users = userService.queryByRoomNo(roomNo);
			for(User user:users){
				msgData.put("uid", user.getOpenid());
				producer.sendMessage(GsonUtils.gsonString(message));
			}
		}
		
	}
	@Override
	public ResultInfo grab(String uid,String pointid) {
		
		Integer pointUSerId = pointDetailMapper.selectPointDetailBy(pointid, uid);
		if(pointUSerId!=null){
			return new ResultInfo(ResultEnum.REDPACKETSHASGRAB);
		}
		
		Integer id = pointDetailMapper.selectPointDetailBy(pointid,"");
		if(id!=null){
			int count = pointDetailMapper.updatePointDetail(id,uid);
			if(count>0){
				return new ResultInfo(ResultEnum.SUCCESS);
			}
		}else{
			return new ResultInfo(ResultEnum.SUCCESS.getCode(),"红包已经抢完");
		}
		return new ResultInfo(ResultEnum.SERVICEERROR);
	}
	@Override
	public PointDetailInfo queryDetail(String pointId) {
		
		List<PointDetailListInfo> infos = pointDetailMapper.list(pointId);
		PointDetailInfo detailInfo = pointMapper.selectByPramry(pointId);
		detailInfo.setInfos(infos);
		return detailInfo;
	}
}
